angular.module('app').directive('myDatePicker', function(){
  return {
    restrict:'EA',
    replace: true,
    scope: {
      data: '=',
      dateRange: '=',
      isEdit: '=',
      excludeWeekend: '=',
      isPermanent:'=',
    },
    templateUrl:'public/js/directive/datepicker.html',
    controller: function($scope){

      $scope.clickDay = function (idx, object) {
        var obj = object.hasOwnProperty(idx);
        if (obj) {
          delete object[idx];
        }else{
          object[idx] = true;
        }
      }

      //转换，将数组转化为对象，以数组元素作Key，值为true
      $scope.formatDatas = function (obj) {
        if (!obj) {
          tmp = {};
        }else if (obj.constructor === Array) {
          var tmp = {};
          angular.forEach(obj, function (val, idx) {
            this[val] = true;
          },tmp);
        }
        return tmp;
      }

      $scope.tem = {};
      $scope.pick = {};
      $scope.week = [
        {idx:1, value:"周日"},
        {idx:2, value:"周一"},
        {idx:3, value:"周二"},
        {idx:4, value:"周三"},
        {idx:5, value:"周四"},
        {idx:6, value:"周五"},
        {idx:7, value:"周六"}
      ];
      $scope.pick.excludeWeekend = $scope.excludeWeekend;
      if (!$scope.dateRange) {
        $scope.dateRange = {};
      }
      if (!$scope.data) {
        $scope.data = {};
      }
      var dftDate = new Date();
      dftDate.setFullYear(2000);
      dftDate.setMonth(1);
      dftDate.setDate(1);

      var today = new Date();
      today.setDate(today.getDate()-1);
      today.setHours(23);
      today.setMinutes(59);
      today.setSeconds(59);
      var now = new Date();

      $scope.set = {
          periods: []
      };
      $scope.view = {
          periods: []
      };
      $scope.dateOptions = {
        formatYear: 'yy',
        startingDay: 1,
        minDate: dftDate,
        maxDate:new Date(now.getTime()+10*365*3600*24*1000),
      };
      $scope.opened = {
        startDate: false,
        endDate: false
      };
      $scope.date = {
        now: new Date(),
        nows: {
          year: new Date().getFullYear(),
          day: new Date().getDate(),
          month: new Date().getMonth() + 1
        },
        week: ["日", "一", "二", "三", "四", "五", "六"],
        dates: [],
        i: 0,
        year: new Date().getFullYear(),
        day: new Date().getDate(),
        month: new Date().getMonth() + 1,
      };

      $scope.open = function($event, a) {
        $event.preventDefault();
        $event.stopPropagation();
        $scope.opened[a] = true;
      };

      $scope.disabled = function(date, mode) {
        return false;
      }

      $scope.dateFn = function(y, m) { //日历选择器
        $scope.date.dates = [];
        firstdate = y + "-" + m + "-" + "1" + " 00:00:00"
        firstdate = new Date(firstdate);
        firstday = firstdate.getDay();
        ny=y;
        nm=m;
        if (m >= 12) { //
          nm = 1;
          ny++;
        } else {
          nm++
        }
        nextMonth = ny + "-" + nm + "-" + "1" + " 00:00:00";
        nextMonth = new Date(new Date(nextMonth).getTime() - 1000 * 60 * 60 * 24);
        lastdate = nextMonth.getDate();
        lastday = nextMonth.getDay();
        et = new Date(new Date().getTime() + 2 * 365 * 1000 * 60 * 60 * 24);
        var ty = $scope.date.nows.year,
        tm = $scope.date.nows.month,
        td = $scope.date.nows.day,
        ey = et.getFullYear(),
        em = et.getMonth() + 1,
        ed = et.getDate();
        for (i = 0, j = lastdate - lastday + 5 + firstday; i <= j; i++) { //
          if (i < (firstday - 1)) {
            $scope.date.dates.push({
              day: ""
            });
          } else if (i >= lastdate + firstday) {
            $scope.date.dates.push({
              day: ""
            });
          } else {
            var color = 0;
            if ((y < ty && m < tm) || (y == ty && m == tm && (i - firstday + 1) < td) || (y >= ey && m > em) || (y==ey && m==em && (i - firstday + 1) > ed)) color = 1;
            trues = [];
            trues[0] = y;
            trues[1] = m > 9 ? m : "0" + m;
            trues[2] = (i - firstday + 1) > 9 ? (i - firstday + 1) : "0" + (i - firstday + 1);
            trues = trues.join('-');
            $scope.date.dates.push({
              day: i - firstday + 1,
              ture: trues,
              color: color
            });
          }
        }
      }

      $scope.datego = function(a) {
        tem = $scope.date.i + a;
        if (tem >= 0 && tem < 6) { //
          $scope.date.i += a;
          $scope.date.month += a;
          if ($scope.date.month > 12) { //
            $scope.date.month = 1;
            $scope.date.year++;
          } else if ($scope.date.month < 1) { //
            $scope.date.month = 12;
            $scope.date.year--;
          }
          $scope.dateFn($scope.date.year, $scope.date.month);
        }
      };

      $scope.dateFn($scope.date.year, $scope.date.month);

      $scope.$watch('pick.excludeWeekend', function (newVal, oldVal) {
        if ( newVal !== oldVal) {
          console.log("excludeWeekend = " + newVal);
          $scope.excludeWeekend = newVal;
        }
      });

      $scope.dateRange.startDate = parseDate($scope.dateRange.startDate||now);
      $scope.dateRange.endDate = parseDate($scope.dateRange.endDate||now);

      $scope.dateRange.effectDates = $scope.formatDatas(arrayMap($scope.dateRange.effectDates, function (key) {
        return key.split(/\s+/)[0];
      }));
      $scope.dateRange.excludeWeekly = $scope.formatDatas($scope.dateRange.excludeWeekly);
      $scope.dateRange.includeWeekly = $scope.formatDatas($scope.dateRange.includeWeekly);
      $scope.dateRangeCategory = dateRangeCategory;
    },
  };
});
